package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override//查询今日佳人
    public RecommendUser findMaxScore(Long toUserId) {
        //1.构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2.构建Query
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override//查询首页推荐数据列表
    public List<RecommendUser> findRecommendationList(Long toUserId, Integer page, Integer pagesize) {
        //1.构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2.构建Q
        Query query = Query.query(criteria);
        //3.查询总数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4.向Query构建分页参数
        query.with(Sort.by(Sort.Order.desc("score")))
                .limit(pagesize)
                .skip((page - 1) * pagesize);
        //5.进行查询
        return mongoTemplate.find(query, RecommendUser.class);
    }

    @Override//查询匹配指数
    public RecommendUser find(Long userId, Long toUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }
}
